// https://leetcode.cn/problems/maximum-erasure-value/description/
class Solution {
public:
    int maximumUniqueSubarray(vector<int>& nums) {
        unordered_map<int,int> dict;
        int n = nums.size();
        int res = 0;
        int temp = 0;
        for(int i = 0, j = 0; j < n; j++){
            dict[nums[j]]++;
            temp += nums[j];
            if(dict[nums[j]] == 2){
                while(nums[i] != nums[j]){
                    dict[nums[i]]--;
                    temp -= nums[i];
                    i++;
                }
                temp -= nums[i];
                dict[nums[i++]]--;
            } 
            res = max(res,temp);
        }
        return res;
    }
};
